(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取下拉框类型
	function getlinklist(selectlist,selectid,type){
	   $.ajax({
			url:'http://web.icecn.net/user/userLists.json',
			type:'POST',
			async:true,
			data:{
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
					if(type==1){
				      dataset=data.t.registerType;
					}else{
					  dataset=data.t.userStatus;
					}
					if(dataset && dataset.length > 0){
					   var html = template(selectlist,{dataset:dataset});
					   $("#"+selectid).html(html);
					};
				}
			}
	   });	
	}
	//获取列表
	function getlist(curpage,pagenum){
		var beginDate=$("#datepicker").val()!=""?DateToUnix($("#datepicker").val())+"000":"";
		var endDate=$("#datepicker2").val()!=""?DateToUnix($("#datepicker2").val())+"000":"";
		var userName=$("#userName").val();
		var mobile=$("#mobile").val();
		var gameRole=$("#gameRole").val();
		var userStatus=$("#userStatus").val();
		var registerType=$("#registerType").val();
	   $.ajax({
			url:'http://web.icecn.net/user/userLists.json',
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				beginDate:beginDate,
				endDate:endDate,
				userName:userName,
				mobile:mobile,
				gameRole:gameRole,
				userStatus:userStatus,
				registerType:registerType
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.userInfos.result;
					registerType=data.t.registerType
					userStatus=data.t.userStatus
					if(dataset && dataset.length > 0){
					   for(var i=0;i<dataset.length;i++){
						   for(var j=0;j<registerType.length;j++){
							 if(dataset[i].registerType==registerType[j].type){
								dataset[i].registerType=registerType[j].desc
							 }
						   }
						   for(var k=0;k<userStatus.length;k++){
							 if(dataset[i].status==userStatus[k].status){
								dataset[i].status=userStatus[k].desc
							 }
						   }
						   dataset[i].registerTime=getMyDate(dataset[i].registerTime)
					   }
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.userInfos.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>");
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".searchbtn").click(function(){
				curpage=1
				getlist(curpage,pagenum)
			})
			//全选
			$(".content").on("click", '.allcheck', function() {
				if ($(".allcheck").attr("checked")) { 
				    $(".allcheck").attr("checked", false)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", false);  
					});  
				} else {  
				    $(".allcheck").attr("checked", true)
					$("input[name=items]").each(function() {  
						$(this).attr("checked", true);  
					});  
				}  
			})
			//修改状态
			$(".content-wrapper").on("click", '.status', function() {
				$("#modal-status .modal-body").attr("data-status",$(this).attr("data-status"))
				$("#modal-status .modal-body").html("确认"+$(this).html()+"?")
			})
			//确认修改状态
			$(".btnstatus").click(function(){
			   var status=$("#modal-status .modal-body").attr("data-status");
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/user/setUserStatus.json',
					type:'POST',
					async:true,
					data:{
						ids:id,
						status:status
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-status").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//解绑用户游戏ID
			$(".btngame").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1,2)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/game/deleteUserGameAccount.json',
					type:'POST',
					async:true,
					data:{
						uid:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-game").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//解绑用户邮箱
			$(".btnemail").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/user/unBindEmail.json',
					type:'POST',
					async:true,
					data:{
						uids:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-email").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//设置为客服
			$(".btnkefu").click(function(){
			   id="";
					$("input[name=items]").each(function() {
						if ($(this).prop("checked")) {
							id += ","+$(this).val();
						}
					}); 
			   id=id.substr(1,2)
			   if(id==""){
			     alert("请选择数据");
				 return;
			   }
			   $.ajax({
					url:'http://web.icecn.net/user/setOfficialUser.json',
					type:'POST',
					async:true,
					data:{
						uid:id,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-kefu").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//更换绑定手机号
			$(".content-wrapper").on("click", '.cphone', function() {
				$("#modal-cphone .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-cphone .modal-body h4").html("更换"+$(this).attr("data-userName")+"的手机号")
			})
			//确认更换绑定手机号
			$(".btncphone").click(function(){
			   var mobile=$("#newmobile").val();
			   var id=$("#modal-cphone .modal-body").attr("data-id")
			   $.ajax({
					url:'http://web.icecn.net/user/changeUserMobile.json',
					type:'POST',
					async:true,
					data:{
						uid:id,
						mobile:mobile
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-cphone").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//升级为代练
			$(".content-wrapper").on("click", '.power', function() {
				$("#modal-power .modal-body").attr("data-id",$(this).attr("data-id"));
				$("#levelingType option:first-child").prop("selected", 'selected');
				$("input[name=gameType]").each(function() {  
						$(this).attr("checked", false);  
				}); 
			})
			//确认升级为代练
			$(".btnpower").click(function(){
			   var id=$("#modal-power .modal-body").attr("data-id")
			   var levelingType=$("#levelingType").val();
			   var gameType="";
					$("input[name=gameType]").each(function() {
						if ($(this).prop("checked")) {
							gameType += ","+$(this).val();
						}
					}); 
			   gameType=gameType.substr(1)
			   if(gameType==""){alert("请选择一个游戏");return}
			   $.ajax({
					url:'http://web.icecn.net/user/upodateLeveling.json',
					type:'POST',
					async:true,
					data:{
						uid:id,
						levelingType:levelingType,
						gameType:gameType,
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-power").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
			getlinklist("selectlist","registerType",1);
			getlinklist("selectlist2","userStatus",2);
		}
	};
	Page.init();
})();